#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>
using namespace std;

mutex mtx;
condition_variable cv;
int tCount = 0;
int result = 1;
const int MAX_COUNT = 100;

void threadA() {
    while (true) {
        unique_lock<mutex> lock(mtx);
        cv.wait(lock, [] { return tCount % 3 == 0 || result > MAX_COUNT; });
        if (result > MAX_COUNT) break;
        cout << "A: " << result << " ";
        result++;
        tCount++;
        cv.notify_all();
    }
}

void threadB() {
    while (true) {
        unique_lock<mutex> lock(mtx);
        cv.wait(lock, [] { return tCount % 3 == 1 || result > MAX_COUNT; });
        if (result > MAX_COUNT) break;
        cout << "B: " << result << " ";
        result++;
        tCount++;
        cv.notify_all();
    }
}

void threadC() {
    while (true) {
        unique_lock<mutex> lock(mtx);
        cv.wait(lock, [] { return tCount % 3 == 2 || result > MAX_COUNT; });
        if (result > MAX_COUNT) break;
        cout << "C: " << result << " ";
        result++;
        tCount++;
        cv.notify_all();
    }
}

int main() {
    thread A(threadA);
    thread B(threadB);
    thread C(threadC);

    A.join();
    B.join();
    C.join();
    return 0;
}
